package com.cgt.xiaotao.order.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cgt.xiaotao.order.domain.po.Order;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface OrderMapper extends BaseMapper<Order> {

    @Select("SELECT coupon_ids FROM `order` WHERE order_no = #{orderNo} AND user_id = #{userId}")
    String selectCouponIdsByOrderNoAndUserId(String orderNo, Long userId);

    @Select("<script>" +
            "SELECT * FROM `order` WHERE user_id = #{userId} " +
            "<if test='status != null'>AND status = #{status}</if> " +
            "ORDER BY ${sortBy} ${sortOrder} " +
            "LIMIT #{offset}, #{pageSize}" +
            "</script>")
    List<Order> selectPageWithSql(@Param("userId") Long userId,
                                  @Param("status") Byte status,
                                  @Param("sortBy") String sortBy,
                                  @Param("sortOrder") String sortOrder,
                                  @Param("offset") long offset,
                                  @Param("pageSize") long pageSize);

    @Select("<script>" +
            "SELECT COUNT(*) FROM `order` WHERE user_id = #{userId} " +
            "<if test='status != null'>AND status = #{status}</if>" +
            "</script>")
    long countWithSql(@Param("userId") Long userId,
                      @Param("status") Byte status);

}
